package com.hzhr.extdevice.controller;

import com.hzhr.extdevice.mapper.device.DevMapper;
import com.hzhr.extdevice.model.OcDeviceOptModel;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class OcDeviceOptController {
    @Autowired
    DevMapper devMapper;

    //获取第一个状态为正常的操作
    @RequestMapping(value = "/oc-device-opt/getFirstNormalOpt", method = RequestMethod.POST)
    public OcDeviceOptModel getDevice() {
        Map<String, Object> opt = devMapper.select1();

        if (opt == null) return null;
        OcDeviceOptModel result = new OcDeviceOptModel();
        result.setId(HzhrTypeUtil.object2integer(opt.get("ID")));
        result.setDes(HzhrTypeUtil.object2string(opt.get("DES")));
        result.setImei(HzhrTypeUtil.object2string(opt.get("IMEI")));
        result.setIotPlatformDeviceId(HzhrTypeUtil.object2string(opt.get("IOT_PLATFORM_DEVICE_ID")));
        result.setOpt(HzhrTypeUtil.object2integer(opt.get("OPT")));
        result.setStatus(HzhrTypeUtil.object2integer(opt.get("STATUS")));
        return result;
    }

    @RequestMapping(value = "/oc-device-opt/delOpt", method = RequestMethod.POST)
    public void deleteDevice(@RequestParam("id") Integer id) {
        //删除oc设备
        devMapper.delete1(id);
    }

    @RequestMapping(value = "/oc-device-opt/updateOpt", method = RequestMethod.POST)
    public void updateDevice(@RequestParam("id") Integer id, @RequestParam("status") String status) {
        //更新oc设备的状态
        devMapper.updateStatus(id, status);
    }
}
